package com.netease.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.netease.ssm.cons.CommonConstant;
import com.netease.ssm.meta.Content;
import com.netease.ssm.meta.Product;
import com.netease.ssm.meta.Trx;
import com.netease.ssm.meta.User;
import com.netease.ssm.service.ProductListService;
import com.netease.ssm.service.ProductService;
import com.netease.ssm.service.UserService;

@Controller
public class HomeController extends BaseController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductListService productListService;
	@Autowired
	UserService userService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView showAllProduct(HttpServletRequest request) {
		List<Content> contentList = productListService.getAllProduct();
		List<Product> productList = new ArrayList<Product>();
		for (Content content : contentList) {
			Trx trx = productService.getTrxByContentId(content.getId());
			System.out.println(content.getId());
			String isBuy = "false";
			String isSell = "false";
			Long buyPrice = (long) 0;
			Product product = new Product();
			if (trx != null) {
				isSell = "true";
				isBuy = "true";
				buyPrice = trx.getPrice();
				product.setId(content.getId());
				product.setTitle(content.getTitle());
				product.setImage(new String(content.getImage()));
				product.setSummary(content.getSummary());
				product.setPrice(content.getPrice());
				product.setIsSell(isSell);
				product.setIsBuy(isBuy);
				product.setBuyPrice(buyPrice);
				productList.add(product);

			} else 
			{
				product.setId(content.getId());
				product.setTitle(content.getTitle());
				product.setImage(new String(content.getImage()));
				product.setSummary(content.getSummary());
				product.setPrice(content.getPrice());
				productList.add(product);
			}

		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName("index");
		mav.addObject("productList", productList);
		return mav;
	}
}
